#include "command.h"
#include "sqlitedb.h"

Command::Command(QObject *parent) : QObject(parent)
{

}

//      Class Flag
int Command::classFlag() const
{
    return m_classFlag;
}
void Command::setClassFlag(const int &classFlag)
{
    m_classFlag = classFlag;
}

//      Command Flag
int Command::commandFlag() const
{
    return m_commandFlag;
}

void Command::setCommandFlag(const int &commandFlag)
{
    m_commandFlag = commandFlag;
}

//          variableList
QStringList Command::variableList() const
{
    return m_variableList;
}
void Command::addVariableList(const QString & variable)
{
    m_variableList.push_back(variable);
}

//获取需要显示在qml界面上的代码条
QString Command::getShowData()
{
    QString title = m_commandName;
    for(int i= 0; i< m_variableList.size(); i++)
    {
        title +=(" "+ m_variableList.at(i));
    }
    qDebug() << title;
    return title;

}
//将qml上的代码处理并存储在本类中(格式类似为： moveC 100 150 200v)
void Command::dealShowData(QString code)
{
    QString tmp = code;
    m_commandName = code.left(code.indexOf(' ', 0));
    tmp = code.right(code.size() - code.indexOf(' ', 0)-1);

    while(tmp!= "")
    {
        QString variable = tmp.left(tmp.indexOf(' ', 0));
        m_variableList.push_back(variable);
        if(!tmp.contains(' '))
            break;
        tmp = tmp.right(tmp.size() - tmp.indexOf(' ', 0) - 1);

    }
}

//            Command Name
QString Command::commandName() const
{
    return m_commandName;
}
void Command::setCommandName(const QString & commandName)
{
    m_commandName = commandName;
}

void Command::initMap(QMap<QString, int> map, QString sql){
    SqliteDB db;
    db.connectDB();
    QStringList list = db.getDBData(sql);
    for(int i=0; i<list.size(); i++)
    {
        map.insert(list.at(list.size()-1-i), i);
    }
    db.closeDB();

    QMap<QString, int>::Iterator iterator;
    for(iterator = map.begin(); iterator != map.end(); iterator++)
        qDebug() << "map.key："+ iterator.key() + "; map.value：" + QString::number(iterator.value(),10);
}
